﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class TabControlPlusStyle1EffectForm : ItemFormTemplate
    {
        public TabControlPlusStyle1EffectForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件效果\\选项卡\\TabControlPlusStyle1EffectForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;

            this.demo_control.TabBar.TabItem.ContentOffset = 9;
            this.demo_control.TabBar.TabItem.LRPadding = new HML.LRPadding(12, 2);
            this.demo_control.TabBar.TabItem.Size = new Size(100, 40);
            this.demo_control.StyleAppearance.Body.BorderColor = Color.FromArgb(110, 153, 153);
            this.demo_control.StyleAppearance.TabBar.TabItem.Normal.BackColor = Color.FromArgb(143, 195, 195);
            this.demo_control.TabItemCreateCustomPathBefore += this.Demo_control_TabItemCreateCustomPathBefore;
            this.demo_control.TabItemDrawBackgroundAfter += this.Demo_control_TabItemDrawBackgroundAfter;
        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
        }

        private void Demo_control_TabItemCreateCustomPathBefore(object sender, TabControlPlusTabItemCreateCustomPathBeforeEventArgs e)
        {
            e.GraphicsPath = new GraphicsPath();
            e.GraphicsPath.AddPolygon(new PointF[] {
            new PointF(e.Bounds_P.X,e.Bounds_P.Y+e.Bounds_P.Height/2),
            new PointF(e.Bounds_P.Right,e.Bounds_P.Top),
            new PointF(e.Bounds_P.Right,e.Bounds_P.Bottom),
            new PointF(e.Bounds_P.X,e.Bounds_P.Bottom)
            });

            e.Cancel = true;
        }

        private void Demo_control_TabItemDrawBackgroundAfter(object sender, TabControlPlusTabItemDrawBackgroundAfterEventArgs e)
        {
            TabControlPlus tabControl = (TabControlPlus)sender;
            int index = tabControl.FindTabPage(e.TabPage);

            if (index > -1 && index < tabControl.TabCount && index >= tabControl.FirstIndex + 1)
            {
                int width = (int)(Math.Ceiling(10 * tabControl.ScaleDpi));
                GraphicsPath gp_prev = new GraphicsPath();
                gp_prev.AddPolygon(new PointF[] {
                new PointF(e.Bounds_P.X,e.Bounds_P.Top),
                new PointF(e.Bounds_P.X,e.Bounds_P.Bottom),
                new PointF(e.Bounds_P.X+width,e.Bounds_P.Bottom)
                });

                GraphicsPath gp_next = new GraphicsPath();
                gp_prev.AddPolygon(new PointF[] {
                new PointF(e.Bounds_P.Right,e.Bounds_P.Top),
                new PointF(e.Bounds_P.Right,e.Bounds_P.Bottom),
                new PointF(e.Bounds_P.Right+width,e.Bounds_P.Bottom)
                });

                Region source_region = e.Graphics.Clip;
                Region tmp_region = new Region(gp_prev);
                if (index == tabControl.TabCount - 1)
                {
                    tmp_region.Union(gp_next);
                }
                tmp_region.Intersect(e.ItemsClipBounds_C);
                e.Graphics.Clip = tmp_region;

                SolidBrush line_sb = new SolidBrush(Color.FromArgb(50, 76, 74, 74));
                e.Graphics.FillPath(line_sb, gp_prev);
                line_sb.Dispose();

                e.Graphics.Clip = source_region;
                tmp_region.Dispose();
            }
        }

        #endregion

    }
}
